Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
A very simple error logger/formatter for Node.
Install as usual with NPM:
npm install --save errorlog
Then configure as the last route of your Express app:
// Get our creator
var errorlog = require('errorlog');
// Create a new logger (with options!)
var log = errorlog(...);
// Foo! Log messages, errors, ...
log('My log message');
The error log can be constructed with the following options:
logger
may be one of the following:
Writable
stream to which error messages will be written to (actually
an object offering a write(...)
function will do).function
that will be invoked once with each message to log.process.stderr
.category
: a category name that will be inserted in the message to log.level
: the minimum level to log messages at
errorlog.TRACE
or 0
: really low priority messages.errorlog.DEBUG
or 100
: debugging messages.errorlog.INFO
or 200
: informational messages (default).errorlog.WARN
or 300
: warning messages.errorlog.ERROR
or 400
: error messages.errorlog.FATAL
or 500
: fatel error messages.errorlog.ALL
or any number smaller than 100
: everything is logged.errorlog.OFF
or any number greater than 500
: disable logging.Options can be specified at construction wrapped in a simple object:
require('errorlog')({
logger: process.stdout,
category: 'my category',
level: 200
});
Or can be specified as a single parameter (either a Writable
stream, a
function
, or a string
identifying the category):
// Forward errors to Winston's "error" facility
var winston = require('winston');
var errorlog = require('errorlog');
// Pass "winston.error" as a writer function
var log = errorlog(winston.error);
The default Writable
stream or logging function
can be configured
using the defaultLog
property:
var errorlog = require('errorlog');
errorlog.defaultLog = process.stdout;
errorlog.defaultLevel = errorlog.DEBUG;
// All logs will use `process.stdout` unless overridden
var log = errorlog('my category');
log('A message to log');
The default for output will be process.stderr
and minimum level ERROR
:
these values will be applied only to loggers created after setting them.
The default level can also be specified by using the LOG_LEVEL
environment
variable.
Needless to say, logging follow the standard util.format
scheme:
log.debug('I have %d %s, and an object %j', 3, 'apples', { foo: 'bar' });
This will produce an entry like
2015-03-31T13:58:06.601Z - DEBUG - I have 3 apples and an object {"foo":"bar"}
Logging levels are associated to various functions of the logger, for example:
var errorlog = require('errorlog');
var log = errorlog({...});
log('Default, logged unless everything is OFF');
log.debug('A debug message');
log.info('Informational message');
log.warn('Some sort of warning');
log.error('Like calling log(...)');
Extra parameters that do not match format characters will be logged in their JSON format, and errors will have their stack traces logged, too. For example:
var extra = { key: 'a simple value' };
var error = new Error('This is an error');
log.error('I have %d %s', 3, 'apples', extra, error);
Will produce something like
2015-03-30T16:45:01.718Z - ERROR - I have 3 apples
>>> {"key":"a simple value"}
Error: This is an error
at Error (native)
at ... stacktrace continues ...
If the configured log
is either process.stderr
(the default) or
process.stderr
, the output to the console will be colorized.
And now I wish GitHub didn't filter the
style
attribute...
This behavior can be changed by setting the errorlog.defaultColorize
property
to false
.
var errorlog = require('errorlog');
errorlog.defaultColorize = false;
log('There you go, some bland output...');
To get an idea of the output, simply run the sample.js
included here.
For log rotation errorlog
plays nicely with packages like
logrotate-stream
.
Copyright (c) 2015 USRZ.com and Pier Paolo Fumagalli
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Yet another logger for Node
The npm package errorlog receives a total of 25 weekly downloads. As such, errorlog popularity was classified as not popular.
We found that errorlog demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.